package server;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.LinkedList;
/**
 * This class provides persistency through text files and serialization.
 * @author Spencer
 */

public class TextInterface {
    
    /**
     * Writes the list of farmers to a text file.
     */
    
	public static void saveFarmers(){
		try	{
		      FileOutputStream saveFile=new FileOutputStream("farmer");
		      ObjectOutputStream save = new ObjectOutputStream(saveFile);
			  save.writeObject(FarmerHandler.farmList);
			  save.close();
		}
		catch ( IOException e)
		{
			System.out.println("File does NOT exist");
		}
		finally{}
		
	}
        
        /*
         * Loads the list of farmers from the text file.
         * 
         * 
         */
	
	public static void loadFarmers(){
		try{
		FileInputStream fi = new FileInputStream("farmer");
		ObjectInputStream loader = new ObjectInputStream (fi);
		FarmerHandler.farmList = (LinkedList<Farmer>) loader.readObject();
		loader.close();
		}
		catch(IOException e)
		{
			System.out.println("IO Exception");			
		} catch (ClassNotFoundException e) {
			System.out.println("File does NOT exist");
		}
		finally{
		}
	}
        
        /**
         * Saves the list of seeds to text files
         */
        
	public static void saveSeeds(){

		try
		{
		      FileOutputStream saveFile=new FileOutputStream("seed");
		      ObjectOutputStream save = new ObjectOutputStream(saveFile);
				save.writeObject(SeedHandler.getSeeds());
			save.close();
		}
		catch ( IOException e)
		{
			System.out.println(e.getMessage());

		}
		finally
		{
	     }
		
	}
	/**
         * Loads the list of seeds from text files
         */
        
	public static void loadSeeds(){
		try{
		FileInputStream fi = new FileInputStream("seed");
		ObjectInputStream loader = new ObjectInputStream (fi);
		SeedHandler.setSeeds((LinkedList<Seed>) loader.readObject());
		loader.close();
		}
		catch(IOException e)
		{
			System.out.println(e.getMessage());

		} catch (ClassNotFoundException e) {
			System.out.println(e.getMessage());

		}
		finally{
		}
	}


}
